var c = require('../lib/common.js');
var cf = require('../lib/config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    c.getLngLatInfo();
    var rid = wx.getStorageSync('userinfo').id;

    this.setData({
      userinfo: wx.getStorageSync('userinfo')
    })
    console.log('rid' + rid);

    if (rid) {
      c.ajax(
        cf.hostapi + 'getriderinfotiid', {
          id: rid
        },
        res => {
          var ismeet = res.data.data.ismeet;
          this.setData({
            ismeet: ismeet
          })
        }
      )
      c.ajax(
        cf.hostapi + 'getridersign', {
          rid: rid
        },
        res => {
          var code = res.data.data;
          if (code == 1) { //没有签到
            this.setData({
              code: 101
            }) //签到按钮显示
          } else { //已经签到了 再判断有没有 签退
            c.ajax(
              cf.hostapi + 'getsigninonInfo', {
                rid: rid
              },
              res => {
                var code = res.data.data;
                if (code == 1) { //没有签退
                  this.setData({
                    code: 1 //签退按钮显示
                  })
                } else { //已经签退
                  this.setData({
                    code: 2 //不可以签退按钮显示
                  })
                }
              }
            )
          }
        }
      )
      //获取今天累计订单
      c.ajax(
        cf.hostapi + 'getOrderCountInfo', {
          rid: rid
        },
        res => {
          this.setData({
            ordernum: res.data.data
          });
        }
      )
      // setTimeout(function () {
      //   that.onLoad();
      // }, 5000)
    } else {
      c.verlogin(this);
    }
  },
  sigclick: function(e) {
    wx.showLoading({
      title: '进行中...',
    })
    var rid = wx.getStorageSync('userinfo').id;
    var that = this;
    c.ajax(
      cf.hostapi + 'signinInfo', {
        rid: rid
      },
      res => {
        var singinid = res.data.data;
        wx.setStorageSync('sid', singinid);
        if (res.data.retcode == 200) {
          wx.hideLoading();
          wx.showToast({
            title: '操作成功',
            image: '/images/dui.png'
          })
          that.onLoad();
          return;
        } else {
          wx.showToast({
            title: '签到失败',
            image: '/images/colsered.png'
          })
          return;
        };
      }
    )
  },

  sigclickout: function() {
    wx.showLoading({
      title: '签退中...',
    })
    var rid = wx.getStorageSync('userinfo').id;

    var that = this;
    c.ajax(
      cf.hostapi + 'getsignoutInfo', {
        rid: rid
      },
      res => {
        if (res.data.retcode == 200) {
          wx.hideLoading();
          wx.showToast({
            title: '签退成功',
            image: '/images/dui.png'
          })
          that.onLoad();
          return;
        } else {
          wx.showToast({
            title: '签退失败',
            image: '/images/colsered.png'
          })
          return;
        };
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  userinfo: function(e) {
    c.clicklogin(e, this);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  kefuclick: function() {
    c.ajax(
      cf.hostapi + 'getphone', {},
      res => {
        var phone = res.data.data.phone;
        wx.showModal({
          title: '',
          content: phone,
          confirmText: '拨打',
          success(res) {
            if (res.confirm) {
              wx.makePhoneCall({
                phoneNumber: phone,
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    )



  },
  allorder: function(e) {
    var status = e.currentTarget.dataset.status;
    wx.navigateTo({
      url: '/pages/allorder/allorder?status=' + status,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  statusclick: function(e) {
    var ismeet = e.currentTarget.dataset.ismeet;
    var rid = wx.getStorageSync('userinfo').id;
    var that = this;
    c.ajax(
      cf.hostapi + 'editIsmeetInfo', {
        ismeet: ismeet,
        rid: rid
      },
      res => {
        if (res.data.retcode == 200) {
          wx.showToast({
            title: '修改成功',
            image: '/images/dui.png'
          })
          that.onLoad();
        };
      }
    )
  }
})